package com.unicom.priceboard.util;

import java.text.DecimalFormat;

/**
 * Creation Date : 2007-12-18
 * @author quan
 */
public class NumberUtil {
	static DecimalFormat df = new DecimalFormat("#,###.##");
	static DecimalFormat dfHose = new DecimalFormat("#,###.0");
	static DecimalFormat dftwo = new DecimalFormat("#,##0.0");
	static DecimalFormat oneDigit_pattern = new DecimalFormat("#,###.#");
	static DecimalFormat oneDigit = new DecimalFormat("#,##0.0");
	static DecimalFormat volume_pattern = new DecimalFormat("#,###");
	static DecimalFormat twoDigitIndex = new DecimalFormat("#,###.00");
	public static String formatHastc_MarketValue(long val) {
		String retVal = "";
		try {
			double dVal = val;
			if(val>1000000000) {
				dVal = (double)val/1000000000;
				retVal = df.format(dVal)+"B";
			}else
				retVal = df.format(dVal);
			return retVal;
		}catch(Exception ex) {
			System.out.println("Error in formating Market Value of HASTC");
			return "";
		}
	}

	public static String formatHose_MarketValue(double val) {

		String retVal = "";
		try {
			double dVal = val;
			if(val>1000000000) {
				dVal = (double)val/1000000000;
				retVal = dfHose.format(dVal)+"B";
			}else
				retVal = dfHose.format(dVal);
			return retVal;
		}catch(Exception ex) {
			System.out.println("Error in formating Market Value of Hose");
			return "";
		}
	}
	/**
	 * @param data
	 * @return formated String of the given number
	 */
	public static String formatOneDigit(String data)
	{
		String retVal = "";

		if(data.equals("0") || data.equals("0.0"))
			return retVal;

		try {
			double dbl = Double.parseDouble(data);
			retVal = oneDigit.format(dbl);
		}catch(Exception ex) {
			System.out.println("error in NumberUtil.formatNumber(String)");
			return "";
		}

		return retVal;
	}
	
	/*
	 * @param data
	 * 
	 * @return formated String of the given number
	 */
	public static String formatTwoDigit(String data) {
		String retVal = "";

		if (data.equals("0") || data.equals("0.00"))
			return retVal;

		try {
			double dbl = Double.parseDouble(data);
			retVal = twoDigitIndex.format(dbl);
		} catch (Exception ex) {
			System.out.println("error in NumberUtil.formatNumber(String)");
			return "";
		}

		return retVal;
	}
	
	public static double formatdouble(double data){
		String valueFormat="";
		try{
			DecimalFormat number = new DecimalFormat("#0.0");
			valueFormat=number.format(data);
		}catch(Exception ex) {
				System.out.println("error in number");
				return 0;
			}


		return Double.valueOf(valueFormat);
	}
	public static String formatNumber(String data) {
		String retVal = "";

		try {
			double dbl = Double.parseDouble(data);
			retVal = oneDigit.format(dbl);
		}catch(Exception ex) {
			System.out.println("error in NumberUtil.formatNumber(String)");
			return "";
		}

		return retVal;
	}


	public static String index(String data) {
		String retVal = "";
		if(data.equals("0"))
			return "";
		try {
			double dbl = Double.parseDouble(data);
			retVal = twoDigitIndex.format(dbl);
		}catch(Exception ex) {
			System.out.println("error in NumberUtil.formatNumber(String)");
			return "";
		}

		return retVal;
	}

	public static String formatTotalTrade(String data) {
		String retVal = "";

		if(data.equals("0"))
			return "";

		try {
			double dbl = Double.parseDouble(data);
			retVal = volume_pattern.format(dbl);
		}catch(Exception ex) {
			System.out.println("error in NumberUtil.formatNumber(String)");
			return "";
		}

		return retVal;
	}

	public static String formatNumberPtd(String data) {
		String retVal = "";

		try {
			double dbl = Double.parseDouble(data);
			retVal = df.format(dbl);
		}catch(Exception ex) {
			System.out.println("error in NumberUtil.formatNumber(String)");
			return "";
		}

		return retVal;
	}
	/**
	 * @param data
	 * @return formated String of the given number
	 */
	public static String formatNumber(double data) {
		String retVal = "";

		if(data==0)
			return "";

		try {
			if(data>999)
				retVal = df.format(data);
			else
				retVal = String.valueOf(data);
		}catch(Exception ex) {
			System.out.println("error in NumberUtil.formatNumber(double)");
			return "";
		}

		return retVal;
	}

	public static String formatLong(long data) {
		try {
			String frmStr = df.format(data);
			return frmStr;
		}catch(Exception ex) {
			System.out.print("illegal data in NumberUtil.formatLong()");
			return "0";
		}
	}

	/**
	 * It's like formatNumber(double data) but the pattern is '#,###.#'
	 * @param data
	 * @return a formated String with only 1 digit after decimal point
	 */
	public static String oneDigitAfterDecimalPoint(double data) {
		String retVal = "";

		try {
			retVal = oneDigit.format(data);
		}catch(Exception ex) {
			System.out.println("error in formating number");
			return "";
		}

		return retVal;
	}

	/**
	 * @param data
	 * @return round up/down 1000 unit values for volume
	 */
	public static String formatMarketVolume(String data) {
		String retVal = "";

		if(!data.equals("")) {
			try {
				double dbl = Double.parseDouble(data);
				if(dbl<100000)
					retVal = df.format(dbl);
				else {
					double realDouble = dbl/1000;
					retVal = df.format(Math.round(realDouble))+"K";
				}
			}catch(Exception ex) {
				System.out.println("error in formating Vol:"+ex.getMessage());
				return "";
			}
		}

		return retVal;
	}

	/**
	 * @param data
	 * @return formated value for volume (#,###)
	 */
	public static String formatVolume(long data) {
		String retVal = "";

		if(data!=0) {
			try {
				retVal = volume_pattern.format(data);
			}catch(Exception ex) {
				System.out.println("error in formating Vol:"+ex.getMessage());
				return "";
			}
		}else
			retVal = "";

		return retVal;
	}
}
